﻿Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndownload.Click
        btndownload.Enabled = False
        Me.txtWeb.Text = Me.HttpGet(Me.txturl.Text.Trim, "", "")
        MessageBox.Show("下载完毕")
        btndownload.Enabled = True
    End Sub

    Private Sub txturl_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txturl.MouseClick
        txturl.SelectAll()

    End Sub

    Private Function HttpGet(ByVal uri As String, ByVal username As String, ByVal password As String) As String
        Dim response As String = Nothing
        Dim webClient As New WebClient
        Using webClient

            If Not (String.IsNullOrEmpty(username) OrElse String.IsNullOrEmpty(password)) Then
                webClient.Credentials = New NetworkCredential(username, password)
            End If
            Try
                Dim smr As New StreamReader(webClient.OpenRead(uri), Encoding.GetEncoding(cbCoding.Text.Trim))
                Return smr.ReadToEnd


                '处理异常
            Catch ex As WebException
                If TypeOf ex.Response Is HttpWebResponse Then
                    Dim status_cod As HttpStatusCode = DirectCast(ex.Response, HttpWebResponse).StatusCode

                    Select Case status_cod
                        Case HttpStatusCode.Unauthorized
                            response = ""
                        Case HttpStatusCode.NotFound
                            response = ""
                        Case Else
                            Throw ex
                    End Select

                End If


                Return response
            End Try
        End Using
        Return response
    End Function







    Private Sub txtWeb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWeb.TextChanged

    End Sub

    Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
        Dim t_form As New htmlView
        t_form.html = txtWeb.Text
        If (t_form.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            txtWeb.Text = t_form.html
        End If


    End Sub

    '开始匹配的代码
    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        txtRes.Text = regMaxString(txtreg.Text, txtWeb.Text, True)
    End Sub

    Private Function regMaxString(ByVal regrule As String, ByVal source As String, ByVal slient As Boolean) As String
        Dim sb As New StringBuilder()
        Try

            Dim reg As New RegularExpressions.Regex(regrule)
            Dim matches As MatchCollection = reg.Matches(source)

            Dim mat_string As New ArrayList()
            If Not slient Then
                MessageBox.Show("共捕获条数为:" & matches.Count)
            End If


            For Each mat As Match In matches
                Dim tpstr As String = mat.Value
                '取出唯一值,这样可以省手工
                If ck_unique.Checked Then  '是否去重
                    If Not mat_string.Contains(tpstr) Then
                        mat_string.Add(tpstr)
                    End If
                Else
                    mat_string.Add(tpstr)
                End If
                If cksep.Checked Then
                    mat_string.Add("---------------" & vbCrLf)
                    mat_string.Add("group 值")


                    Dim count As Integer = mat.Groups.Count
                    mat_string.Add("title is:" & mat.Groups("title").Value & vbCrLf)
                End If

                'For i As Integer = 0 To count
                '    mat_string.Add(i.ToString() & "|" & mat.Groups(i).Value.ToString & vbCrLf)
                'Next


            Next


            '组合成字符串

            For Each Str As String In mat_string
                sb.AppendLine(Str)
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return sb.ToString
    End Function

    Private Sub txtWeb_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWeb.KeyUp
        '让文件框支持ctrl+a
        If e.Modifiers = Keys.Control And e.KeyCode = Keys.A Then
            Dim tb As TextBox = sender
            tb.SelectAll()
        End If
    End Sub

    Private Sub btnResShow(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t_form As New htmlView
        t_form.html = txtRes.Text
        t_form.ShowDialog()
    End Sub

    Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Click
        Dim tp As New Form2
        tp.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim tpwebb As New BrowserForm

        If tpwebb.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtWeb.Text = tpwebb.webSource
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'MessageBox.Show(Now.ToString(TextBox1.Text.Trim))
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim bd As New baiduget
        bd.ShowDialog()
    End Sub

    Private Sub btnreplace_Click(sender As Object, e As EventArgs) Handles btnreplace.Click
        Dim reg As New Regex(txtreg.Text)
        txtRes.Text = reg.Replace(txtWeb.Text, txt_replace.Text.Trim)
    End Sub

    Private Sub btn_spider_Click(sender As Object, e As EventArgs) Handles btn_spider.Click
        Dim myspider As New mycrawler
        myspider.Show()
    End Sub

    Private Sub btnregfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnregfile.Click
        btnregfile.Enabled = False

        Dim sr As New StreamReader(txtregfile.Text)
        Dim sb As New StringBuilder
        Dim ressb As New StringBuilder
        Do While (sr.Peek > -1)
            Dim con As String = sr.ReadLine
            If (con <> "--------------------------------------") Then
                sb.Append((con.Trim & ChrW(13) & ChrW(10)))
            Else
                ressb.Append(regMaxString(txtreg.Text, sb.ToString(), True))
                sb = New StringBuilder()
            End If
        Loop

        '保存正则结果
        Dim filename As String = (DateTime.Now.Minute.ToString & DateTime.Now.Second & ".txt")
        Dim sw As New StreamWriter((Application.StartupPath & "\" & filename), True)
        sw.WriteLine(ressb.ToString())
        sw.Flush()
        sw.Close()



        MessageBox.Show("done")
        btnregfile.Enabled = True
    End Sub

    Private Sub btndownImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndownImage.Click
        Dim temp As New downimage
        temp.Show()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim tp As New freplace
        tp.Show()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim tps As New spider()
        tps.Show()
    End Sub
End Class
